package es.titoapps.spanishhandball.iu.centraldedatos;

import es.titoapps.spanishhandball.R;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;


public class CentralDeDatos extends Fragment {

	private static final int CLASIFICACION_STATE = 0x1;
    private static final int RESULTADOS_STATE = 0x2;
	
    private int mTabState;
    
	private LinearLayout tabButtonClasificacion,tabButtonResultados;
	
	/** Se lanza al crear la actividad */
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
	
		super.onCreateView(inflater, container, savedInstanceState);
		
		View view=inflater.inflate(R.layout.centraldedatoslayout, container, false);
		
		tabButtonClasificacion=(LinearLayout)view.findViewById(R.id.layoutTabClasificacion);
		tabButtonResultados=(LinearLayout) view.findViewById(R.id.layoutTabResultados);
		
		tabButtonClasificacion.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // Switch the tab content to display the list view.
                gotoClasificacion();
            }
        });
        
		tabButtonResultados.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // Switch the tab content to display the grid view.
                gotoResultados();
            }
        });
		
		
		gotoClasificacion();
//		Intent serviceIntent = new Intent();
//		serviceIntent.setAction("es.titoapps.spanishhandball.NewsLoaderService");
//		getActivity().startService(serviceIntent);
		
		return view;
        
    }
    
	private void gotoClasificacion() {
		// mTabState keeps track of which tab is currently displaying its contents.
        // Perform a check to make sure the list tab content isn't already displaying.
        
        if (mTabState != CLASIFICACION_STATE) {
            // Update the mTabState 
            mTabState = CLASIFICACION_STATE;
            
            // Fragments have access to their parent Activity's FragmentManager. You can
            // obtain the FragmentManager like this.
            FragmentManager fm = getFragmentManager();
            
            if (fm != null) {
                // Perform the FragmentTransaction to load in the list tab content.
                // Using FragmentTransaction#replace will destroy any Fragments
                // currently inside R.id.fragment_content and add the new Fragment
                // in its place.
            	ClasificacionActivity newsFragment=new ClasificacionActivity();
//            	Bundle bundle= new Bundle();
//            	bundle.putBoolean("NEWSTAB", false);
//            	newsFragment.setArguments(bundle);
            	tabButtonClasificacion.setSelected(true);
            	tabButtonResultados.setSelected(false);
            	
                FragmentTransaction ft = fm.beginTransaction();
                ft.replace(R.id.tabContent, newsFragment);
                ft.commit();
            }
        }
		
	}

	private void gotoResultados() {
		// mTabState keeps track of which tab is currently displaying its contents.
        // Perform a check to make sure the list tab content isn't already displaying.
        
        if (mTabState != RESULTADOS_STATE) {
            // Update the mTabState 
            mTabState = RESULTADOS_STATE;
            
            // Fragments have access to their parent Activity's FragmentManager. You can
            // obtain the FragmentManager like this.
            FragmentManager fm = getFragmentManager();
            
            if (fm != null) {
                // Perform the FragmentTransaction to load in the list tab content.
                // Using FragmentTransaction#replace will destroy any Fragments
                // currently inside R.id.fragment_content and add the new Fragment
                // in its place.
            	ResultadosActivity newsFragment=new ResultadosActivity();
//            	Bundle bundle= new Bundle();
//            	bundle.putBoolean("NEWSTAB", false);
//            	newsFragment.setArguments(bundle);
            	tabButtonClasificacion.setSelected(false);
            	tabButtonResultados.setSelected(true);
            	
                FragmentTransaction ft = fm.beginTransaction();
                ft.replace(R.id.tabContent, newsFragment);
                ft.commit();
            }
        }
	}
}
